
import React, { Component } from 'react';
import { WeaAuth, WeaLocaleProvider } from 'ecCom';
import { observer } from 'mobx-react';
import { message } from 'antd';
import * as mobx from 'mobx';
const getLabel = WeaLocaleProvider.getLabel;

@observer
export default class AddShare extends Component {

    constructor(props) {
        super(props);
    }

    componentDidMount () {
        const { wfAddShareStore } = this.props;
        wfAddShareStore.getShareCondition();
    }

    render () {

        const { wfAddShareStore } = this.props;
        const { visible, conditions } = wfAddShareStore;

        return (
            <WeaAuth ecId={`${this && this.props && this.props.ecId || ''}_WeaAuth@1gleon`}
                ref={auth => this.auth = auth}
                visible={visible}
                onOkClose={false}
                height={300}
                width={600}
                conditions={mobx.toJS(conditions)}
                title={getLabel(383655,'添加共享')}
                icon="icon-coms-workflow"
                iconBgcolor='#0079DE'
                onOk={this.onOk}
                onCancel={() => {
                    wfAddShareStore.setState({ visible: false });
                }}
            />
        )
    }

    onOk = v => {

        const { wfAddShareStore } = this.props;
        const { conditions } = wfAddShareStore;
        const { objTypeValue, adviceValue : iscanread,  objValue,
            jobsLevel : joblevel, relatedshareid_6, from_Level : seclevel, to_Level : seclevelMax, objValuespan, relatedshareid_6span} = v;


        if(seclevelMax < seclevel){
            message.warning(getLabel(385085,'安全级别设置有误，请重新设置'));
            return ;
        }

        const currentItem = conditions[1][objTypeValue];

        let urls = {};
        currentItem.forEach(item => {
            urls = {...urls,...this.getLinkUrls(mobx.toJS(item),joblevel)};
        })

        let permissiontype;

        switch (Number(objTypeValue)){
            case 194 :
                permissiontype = 6;
                break ;
            case 0 :
                permissiontype = 3;
                break ;
            case 17 :
                permissiontype = 5;
                break ;
            case 65 :
                permissiontype = 2;
                break ;
            case 278 :
                permissiontype = 7;
                break ;
            default :
                permissiontype = 1;
                break ;
        }

        let allObjInfo = [], jobObjInfo = [];
        if(objValuespan){
            for(let [ index, value ] of objValue.split(',').entries()){
                let obj = {
                    id : value,
                    link : `${urls.objValue}${value}`,
                    name : objValuespan.split(',')[index],
                };
                allObjInfo.push(obj);
            }
        }
        if(relatedshareid_6){
            for(let [ index, value ] of relatedshareid_6.split(',').entries()){
                let obj = {
                    id : value,
                    link : `${urls.relatedshareid_6}${value}`,
                    name : relatedshareid_6span.split(',')[index],
                };
                jobObjInfo.push(obj);
            }
        }

        typeof this.props.onOk === 'function' && this.props.onOk({
            permissiontype,
            iscanread,
            objValue,
            seclevel,
            seclevelMax,
            rolelevel : joblevel,
            joblevel,
            relatedshareid_6
        }, allObjInfo, jobObjInfo)

        wfAddShareStore.setState({visible : false});
        this.auth.onCancel();
    }

    //过滤urls
    getLinkUrls = (datas = [], joblevel) => {
        let urls = {};
        const getNewObj = d => {
            if (d.conditionType === 'SELECT_LINKAGE') {
                const sDatas = d.selectLinkageDatas;
                const bDatas = sDatas[joblevel];
                bDatas && getNewObj(bDatas);
            } else if (d.conditionType === 'BROWSER') {
                if (d.browserConditionParam.linkUrl) {
                    urls[d.domkey[0]] = d.browserConditionParam.linkUrl;
                }
            }
        };
        if (typeof datas === 'object') {
            if (Object.prototype.toString.call(datas) === '[object Array]') {
                datas.forEach(data => {
                    getNewObj(data);
                });
            } else {
                for (const key in datas) {
                    if (datas.hasOwnProperty(key)) {
                        getNewObj(datas[key]);
                    }
                }
            }
        }
        return urls;
    };


}